package com.gjy.leetcode;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * 557. 反转字符串中的单词 III
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-06 09:30:34
 */
public class A557 {

    @Test
    public void test1() {
        String s = "Let's take LeetCode contest";
        String s1 = reverseWords2(s);
        assertEquals("s'teL ekat edoCteeL tsetnoc", s1);
    }

    public String reverseWords2(String s) {
        return Arrays.stream(s.split(" "))
                .map(s1 -> new StringBuilder().append(s1).reverse().toString())
                .collect(Collectors.joining(" "));
    }

    public String reverseWords(String s) {
        String[] strings = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            sb.append(new StringBuilder().append(strings[i]).reverse());
            if (i != strings.length - 1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }
}
